package com.ubtechinc.cruzr.libai.honglauncher;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 *页标
*@author :ruiqiang
*/
public class PageIndicate extends LinearLayout {
	private static final int PADDING = 20;
	private static final int HEIGHT = 30;
	private int mItemWidth = 18;
	
	public PageIndicate(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public PageIndicate(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public PageIndicate(Context context) {
		super(context);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int count = getChildCount();
		int mViewWidth = mItemWidth * count + (count - 1) * PADDING;
		setMeasuredDimension(mViewWidth, HEIGHT);
		for (int i = 0; i < count; i++) {
			View child = getChildAt(i);
			child.measure(MeasureSpec.makeMeasureSpec(mItemWidth, MeasureSpec.EXACTLY),
					MeasureSpec.makeMeasureSpec(mItemWidth, MeasureSpec.EXACTLY));

		}
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		for(int i = 0 ; i < getChildCount(); i++){
			View child = getChildAt(i);
			if(child != null){
				int left = i * mItemWidth + (i * PADDING);
				child.layout(left , 0, left + mItemWidth, mItemWidth);
			}
		}
	}
	public void reFreshPage(Context context, int curpage, int totalpage){
		if (totalpage > 1) {
			if (getChildCount() != totalpage) {
				removeAllViews();
				for (int i = 0; i < totalpage; i++) {
					ImageView v = new ImageView(context);
					if (i == curpage) {
						v.setBackgroundResource(R.drawable.icon_pager_sel);
					} else {
						v.setBackgroundResource(R.drawable.icon_pager_nor);
					}
					addView(v);
				}
			} else {
				for(int i = 0;i<getChildCount();i++){
					ImageView v = (ImageView) getChildAt(i);
					if (i == curpage) {
						v.setBackgroundResource(R.drawable.icon_pager_sel);
					} else {
						v.setBackgroundResource(R.drawable.icon_pager_nor);
					}
				}
			}
		} else {
			removeAllViews();
		}
	}
}
